//
//  HWCommentCircleCell.m
//  嗨文
//
//  Created by 响当当 on 6/24/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "HWCommentCircleCell.h"

@interface HWCommentCircleCell()<UITextFieldDelegate,UITextViewDelegate>
@property (nonatomic ,strong)UITextField *titleTextField;//标题
@property (nonatomic, strong)UITextView *contextView;
@end

@implementation HWCommentCircleCell

- (UITextField *)titleTextField
{
    if (!_titleTextField)
    {
        _titleTextField = [[UITextField alloc]initWithFrame:CGRectMake(5*kUIScaleBasePlus,0,SCREEN_WIDTH-10*kUIScaleBasePlus,30*kUIScaleBasePlus)];
        _titleTextField.placeholder = @"请输入标题";
        _titleTextField.textColor = LCHexColor(0x333333);
        _titleTextField.font = [UIFont systemFontOfSize:15*kUIScaleBasePlus];
        _titleTextField.delegate = self;
        [_titleTextField addTarget:self action:@selector(textchange:) forControlEvents:UIControlEventAllEvents];
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(_titleTextField.frame),SCREEN_WIDTH,1*kUIScaleBasePlus)];
        lineView.backgroundColor = JSALineColor;
        [self.contentView addSubview:lineView];
    }
    return _titleTextField;
}

- (UITextView *)contextView
{
    if (!_contextView)
    {
        _contextView = [[UITextView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(self.titleTextField.frame)+1*kUIScaleBasePlus,SCREEN_WIDTH,230*kUIScaleBasePlus)];
        _contextView.delegate = self;
        _contextView.font = [UIFont systemFontOfSize:18*kUIScaleBasePlus];
        _contextView.keyboardType = UIReturnKeyDefault;
        _contextView.textAlignment = NSTextAlignmentLeft;
        _contextView.textColor = LCHexColor(0x333333);
        _contextView.placeHolder = @"请写下您的精彩评论,不能超过200字";
    }
    return _contextView;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self.contentView addSubview:self.titleTextField];
        [self.contentView addSubview:self.contextView];
    }
    return self;
}

#pragma mark - delegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    NSString *tobeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    if (tobeString.length > 200 && range.length!=1)
    {
        textField.text = [tobeString substringToIndex:200];
        return NO;
    }
    
    return YES;
}

- (void)textchange:(UITextField *)textField
{
    if (self.getBackTitleBlock)
    {
        self.getBackTitleBlock(textField.text);
    }
}

- (void)textViewDidChange:(UITextView *)textView {
    if (textView.text.length == 0) {
        self.contextView.placeHolder = @"请写下您的精彩评论,不能超过200字";
    }else{
        self.contextView.placeHolder = @"";
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"])
    {
        [textView resignFirstResponder];
        return NO;
    }
    
    
    if (self.getBackContextBlock)
    {
        self.getBackContextBlock(text);
    }
    
    return YES;
}

@end
